# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.historyDao import HistoryDao

from . import app

CORS(app, supports_credentials=True)

'''历史搜索表 zzh_history API'''


@app.route('/add-history', methods=['POST'])
def add_his():
    if not request.json:
        abort(400)
    his_info = {
        "user_id": request.json['user_id'],
        "user_ip": request.json['user_ip'],
        "history_keyword": request.json['keyword'],
        "history_type": request.json['history_type']
    }
    rs = HistoryDao().add_history(his_info)
    if rs:
        return jsonify({"hisInfo": his_info, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/query-history/<int:user_id>', methods=['GET'])
def query_his(user_id):
    rs = HistoryDao().query_his(user_id)

    if rs:
        return jsonify({"hisInfo": rs, "message": {"code": 1, "info": "OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "ERROR"}})


@app.route('/logic-delete-history/<int:user_id>', methods=['POST'])
def logicDeleteHistory(user_id):
    if not request.json:
        abort(400)
    historyId = request.json.get('historyId')

    rs = HistoryDao().logicDelete(historyId,user_id)

    if rs:
        return jsonify({"hisInfo": rs, "message": {"code": 1, "info": "Logic Delete OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "Logic Delete ERROR"}})


